package com.frxuexi.shop2307.shop

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.room.ProductEntity
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.lib_base.utils.LiveDataBus
import com.frxuexi.lib_base.utils.SimpleQRCode
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.adapter.CommentAdapter
import com.frxuexi.shop2307.adapter.FragAdapter
import com.frxuexi.shop2307.databinding.ActivityShopDetailBinding
import com.frxuexi.shop2307.fragment.DetailOneFragment
import com.frxuexi.shop2307.fragment.DetailTwoFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.BarcodeEncoder
import com.youth.banner.loader.ImageLoader
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/ShopDetailActivity")
@AndroidEntryPoint
class ShopDetailActivity : BaseActivity<ActivityShopDetailBinding, ShopViewModel>() {

    val commentAdapter by lazy { CommentAdapter() }

    @JvmField
    @Autowired
    var id: Int = -1
    var isLove = false

    override fun initData() {
        vm.detailData.observe(this) {
            binding.bean = it.productInfo
            GlideUtils.getImage(binding.detailIv, it.productInfo.image)
//            GlideUtils.getImage(binding.detailVideo, it.productInfo.image)
            GlideUtils.getImage(binding.detailAni, it.productInfo.image)
        }
        vm.commenNumData.observe(this) {
            binding.detailNum.setText("全部评论（${it.sumCount}）")
            binding.detailTab.apply {
                addTab(newTab().setText("详情"))
                addTab(newTab().setText("评论（${it.sumCount}）"), true)

                addOnTabSelectedListener(object : OnTabSelectedListener {
                    override fun onTabSelected(tab: TabLayout.Tab?) {
                        if (tab?.position == 0) {
                            binding.detailIv.visibility = View.VISIBLE
                            binding.detailGroup.visibility = View.GONE
                        } else {
                            binding.detailIv.visibility = View.GONE
                            binding.detailGroup.visibility = View.VISIBLE

                        }
                    }

                    override fun onTabUnselected(tab: TabLayout.Tab?) {
                    }

                    override fun onTabReselected(tab: TabLayout.Tab?) {
                    }
                })
            }
        }

        vm.commentData.observe(this) {
            commentAdapter.apply {
                data.clear()
                addData(it.list)
                notifyDataSetChanged()
            }
        }

        vm.addProData.observe(this) {
            isLove = true
            binding.detailLove.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_love)
        }

        vm.isLoveData.observe(this) {
            isLove = true
            binding.detailLove.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_love)
        }

        vm.deleteLoveData.observe(this){
            isLove = false
            binding.detailLove.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_nolove)
        }

        vm.addCartData.observe(this){
            ToastUtils.showLong("加入购物车成功")
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.getShopDetail(id)
        vm.getCommentNum(id)
        vm.isLove(id)

        lifecycleScope.launch {
            vm.getComment(id, 3, 1, "0")
        }

        val bitmap = BarcodeEncoder().encodeBitmap(
            Const.BASE_URL + "/api/front/product/detail/{${id}}?normal=normal",
            BarcodeFormat.QR_CODE,
            200,
            200
        )
        binding.detailVideo.setImageBitmap(bitmap)
//        binding.detailVideo.setImageBitmap(SimpleQRCode.generate(Const.BASE_URL+"/api/front/product/detail/{${id}}?normal=normal",200))
        binding.detailMore.setOnClickListener {
            ArouterManager.nav(this, "/app/CommentActivity", id)
        }

        binding.detailClose.setOnClickListener {
            binding.detailVideoGroup.visibility = View.GONE
        }

        binding.detailVideo.setOnClickListener {
            ArouterManager.nav(this, "/app/ShopVideoActivity")
        }

        binding.detailBack.setOnClickListener {
            finish()
        }

        binding.detailRv.apply {
            adapter = commentAdapter
            layoutManager = LinearLayoutManager(this@ShopDetailActivity)
        }

        val list = listOf<Fragment>(DetailOneFragment(), DetailTwoFragment())
        val fragAdapter = FragAdapter(supportFragmentManager, list)
        binding.detailVp.adapter = fragAdapter

        binding.detailLove.setOnClickListener {
            val entity = ProductEntity(
                id,
                binding!!.bean!!.image,
                binding.bean!!.storeName,
                binding.bean!!.price,
                binding.bean!!.otPrice
            )
            if (isLove) {
                vm.deleteLove(entity)
            } else {
                vm.addPro(
                    entity
                )
            }
            isLove = !isLove
        }

        binding.detailLove.setOnLongClickListener {
            ArouterManager.nav(this,"/app/ShopLoveActivity")
            return@setOnLongClickListener true
        }

        binding.detailAdd.setOnClickListener {
            vm.addCart(mapOf("cartNum" to 1,"productId" to id,"productAttrUnique" to id.toString()) )

            val animator = ObjectAnimator.ofFloat(binding.detailAni, "translationY", -50f)
            val animator2 = ObjectAnimator.ofFloat(binding.detailAni, "translationX", -800f)
            val animator3 = ObjectAnimator.ofFloat(binding.detailAni, "alpha", 1f,0f)
            AnimatorSet().apply {
                    play(animator)
                        .with(animator2)
                        .with(animator3)
                setDuration(3000)
                start()
            }


        }

        binding.detailCart.setOnClickListener {
            LiveDataBus.with("nav",String::class.java).postValue("3")
            finish()
//            ArouterManager.nav(this,"/app/MainActivity",3)
        }

    }

}